package com.ruoyi.web.controller.emp;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 预警结果Controller
 *
 * @author zsl
 * @date 2024-04-15
 */
@RestController
@RequestMapping("/system/result")
@Slf4j
public class EmpEarlyWarnResultController extends BaseController {
    @Autowired
    private IEmpEarlyWarnResultService empEarlyWarnResultService;
    @Autowired
    private IEmpNormCollectService iEmpNormCollectService;
    @Autowired
    private IEmpModelSchemeService iEmpModelSchemeService;
    @Autowired
    private IEmpNormSystemService iEmpNormSystemService;
    @Autowired
    private IEmpAreaService iEmpAreaService;

    /**
     * 查询预警结果列表
     */
    @PreAuthorize("@ss.hasPermi('system:result:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmpEarlyWarnResult empEarlyWarnResult) {
        startPage();
        List<EmpEarlyWarnResult> list = empEarlyWarnResultService.selectEmpEarlyWarnResultList(empEarlyWarnResult);
        return getDataTable(list);
    }

    /**
     * 导出预警结果列表
     */
    @PreAuthorize("@ss.hasPermi('system:result:export')")
    @Log(title = "预警结果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmpEarlyWarnResult empEarlyWarnResult) {
        List<EmpEarlyWarnResult> list = empEarlyWarnResultService.selectEmpEarlyWarnResultList(empEarlyWarnResult);
        log.info("导出预警结果列表:{}", JSON.toJSONString(list));
        ExcelUtil<EmpEarlyWarnResult> util = new ExcelUtil<>(EmpEarlyWarnResult.class);
        util.exportExcel(response, list, "预警结果数据");
    }

    /**
     * 获取预警结果详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:result:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(empEarlyWarnResultService.selectEmpEarlyWarnResultById(id));
    }

    /**
     * 新增预警结果
     */
    @PreAuthorize("@ss.hasPermi('system:result:add')")
    @Log(title = "预警结果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmpEarlyWarnResult empEarlyWarnResult) {
        empEarlyWarnResult.setCreateBy(getUsername());
        return toAjax(empEarlyWarnResultService.insertEmpEarlyWarnResult(empEarlyWarnResult));
    }

    /**
     * 修改预警结果
     */
    @PreAuthorize("@ss.hasPermi('system:result:edit')")
    @Log(title = "预警结果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmpEarlyWarnResult empEarlyWarnResult) {
        return toAjax(empEarlyWarnResultService.updateEmpEarlyWarnResult(empEarlyWarnResult));
    }

    /**
     * 删除预警结果
     */
    @PreAuthorize("@ss.hasPermi('system:result:remove')")
    @Log(title = "预警结果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(empEarlyWarnResultService.deleteEmpEarlyWarnResultByIds(ids));
    }

    /**
     * 导入预警结果
     */
    @Log(title = "预警结果", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:result:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<EmpEarlyWarnResult> util = new ExcelUtil<>(EmpEarlyWarnResult.class);
        List<EmpEarlyWarnResult> list = util.importExcel(file.getInputStream());
        log.info("导入预警结果数量：{}", list.size());
        log.info("导入预警结果：{}", JSON.toJSONString(list));
        String operName = getUsername();
        String message = empEarlyWarnResultService.importEmpEarlyWarnResult(list, updateSupport, operName);
        return success(message);
    }

    /**
     * 下载模块预警结果
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<EmpEarlyWarnResult> util = new ExcelUtil<>(EmpEarlyWarnResult.class);
        util.importTemplateExcel(response, "预警结果数据");
    }

    /**
     * 获取下拉框内容
     *
     * @return
     */
    @PreAuthorize("@ss.hasPermi('system:result:list')")
    @GetMapping(value = "/getCollectAndScheme")
    public AjaxResult getCollectAndScheme() {
        List<EmpNormCollect> empNormCollects = new ArrayList<>();
        List<EmpModelScheme> empModelSchemes = new ArrayList<>();
        try {
            //查询所有指标采集
            empNormCollects = iEmpNormCollectService.list();
            empNormCollects.forEach(item -> {
                //展示的下拉卡内容
                String collectOptionName = "";
                collectOptionName += item.getYear() + "年";
                EmpNormSystem empNormSystem = iEmpNormSystemService.getById(item.getSystemId());
                if (ObjectUtils.isNotEmpty(empNormSystem)) {
                    //属性名称
                    collectOptionName += " -- " + empNormSystem.getName();
                }
                EmpArea empArea = iEmpAreaService.getById(item.getAreaId());
                if (ObjectUtils.isNotEmpty(empArea)) {
                    //区域名称
                    collectOptionName += " -- " + empArea.getName();
                }
                item.setCollectOptionName(collectOptionName);
            });
            //查询所有模型方案
            empModelSchemes = iEmpModelSchemeService.list();
        } catch (Exception e) {
            log.error("查询指标采集下拉框异常：", e);
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("empNormCollects", empNormCollects);
        ajax.put("empModelSchemes", empModelSchemes);
        return ajax;
    }
}
